wordSize = 4;

var ssw_style = 'right';
var ssw_style_h = ssw_style+"-h";

var wordList = initWordList();

function initWordList() {
    var tmp1 = {}
    var tmp0 = [
        'able',
        'aid',
        'air',
        'ant',
        'art',
        'at',
        'be',
        'bean',
        'beat',
        'ball',
        'bee',
        'beet',
        'best',
        'bell',
        'bet',
        'boat',
        'bone',
        'bow',
        'cane',
        'can',
        'cat',
        'car',
        'cart',
        'card',
        'care',
        'cord',
        'curd',
        'clan',
        'cup',
        'cone',
        'call',
        'cane',
        'cake',
        'cold',
        'clip',
        'come',
        'cap',
        'chip',
        'I',
        'it',
        'bat',
        'am',
        'a',
        'sit',
        'sole',
        'song',
        'chat',
        'chin',
        'date',
        'dame',
        'dot',
        'dog',
        'dust',
        'duck',
        'eat',
        'eel',
        'man',
        'mane',
        'map',
        'mare',
        'mast',
        'mat',
        'mate',
        'maze',
        'me',
        'men',
        'rat',
        'ring',
        'rose',
        'tail',
        'tale',
        'tar',
        'tub',
        'vase'
    ];
    tmp0 = tmp0.sort();
    var len = tmp0.length;
    for (var i = 0; i < len; i++) {
        var e = tmp0[i];
        tmp1[e] = e;
    }
    return tmp1;
}


function matchesWordList(word) {
    /*
    var word = '';
    var kk = ' ';
    var clean = false;
    for (var i = 0; i < wordSize; i++) {
        var c = ww[i];
        if (c == ' ') {
            clean = true;            
        }
        if (clean) {
            break;
        }
        word += ww[i];
    }*/
    var matches = [];
    for (var k in wordList) {
        var w = wordList[k];
        var p = w.indexOf(word);
        if (p == 0 && word.length > 0) {
            matches.push(w);
        }
    }
    return matches;
}

function createAlphabetList() {
    var x = {};
    x[0] = ' ';
    for (var i = 0; i < 26; i++) {
        x[i+1] = String.fromCharCode(97 + i);
    }
    return x;
}

function createIndexList() {
    var x = {};
    x[' '] = 0;
    for (var i = 1; i < 27; i++) {
        x[String.fromCharCode(96 + i)] = i;
    }
    return x;
}

alphabet = createAlphabetList();
alphaindices = createIndexList();
    
function letterOfIndex(i) {
    return alphabet[i];
}

function indexOfLetter(c) {
    return alphaindices[c];
}

function getMatches(w, p) {
    var w = SpinningWheel.getSelectedValues().values.join('');
    var word = '';
    for (var i = 0; i < (p+1); i++) {
        word += w[i];
    }
    var matches = matchesWordList(word);
    return matches;
}

function getMatchColumn(matches, place) {
    var set = {};
    var col = {0:' '};
    var len = matches.length;
    for (var x=0; x<len; x++) {
        var match = matches[x];
        var c = match[place];
        if (typeof c == "undefined") {
            continue;
        }
        set[c] = c;
    } 
    var i = 1;
    for (var k in set) {
        col[i] = k;
        i++;
    }
    return col;
}

function getWord() {
    var word = "";
    var w = SpinningWheel.getSelectedValues().values.join('');
    var len = w.length; // cannot be bigger than wordSize
    for (var i = 0; i < len; i++) {
        var c = w[i];
        if (c == " ") {
            return word;
        }
        word += c;
    }
    return word;    
}

function updateOnScroll() {
    var word = getWord();
    var curr = SpinningWheel.activeSlot;
    if (curr >= word.length) {
        curr = word.length - 1;
    }
    var matches = getMatches(word, curr);
    var firstMatch = null;
    if (matches.length > 0) {
        firstMatch = matches[0];
    }
    var next = curr + 1;
    var j = next;
    for (var ii = next; ii<wordSize; ii++) {
        var newlist = alphabet;
        var st = ssw_style;
        if (firstMatch != null) {
            newlist = getMatchColumn(matches, ii); 
            //st = ssw_style_h;
        }
        SpinningWheel.addSlot(newlist, st, 0, ii);
    }
    SpinningWheel.refreshSlots();
    for (var ii = next; ii<wordSize; ii++) {
        var l = Object.keys(SpinningWheel.slotData[ii].values).length;
        l = l > 1 ? 1 : 0;
        SpinningWheel.scrollToValue(ii, l);
    }    
}

function openWords() {
    SpinningWheel.addSlot(alphabet, ssw_style, 1);
    for (var i = 1; i < wordSize; i++) {
        SpinningWheel.addSlot(alphabet, ssw_style, 0);        
    }
    SpinningWheel.setCancelAction(cancel);
    SpinningWheel.setDoneAction(done);
    SpinningWheel.setScrollAction(updateOnScroll);
    SpinningWheel.open();    
}

// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

function fakeScroll(c) {
    
}


/* XXX
function openBirthDate() {
    var now = new Date();
    var days = { };
    var years = { };
    var months = {
        1: 'Gen', 
        2: 'Feb', 
        3: 'Mar', 
        4: 'Apr', 
        5: 'May', 
        6: 'Jun', 
        7: 'Jul', 
        8: 'Aug', 
        9: 'Sep', 
        10: 'Oct', 
        11: 'Nov', 
        12: 'Dec'
    };
	
    for( var i = 1; i < 32; i += 1 ) {
        days[i] = i;
    }

    for( i = now.getFullYear()-100; i < now.getFullYear()+1; i += 1 ) {
        years[i] = i;
    }

    SpinningWheel.addSlot(years, 'right', 1999);
    SpinningWheel.addSlot(months, '', 4);
    SpinningWheel.addSlot(days, 'right', 12);
	
    SpinningWheel.setCancelAction(cancel);
    SpinningWheel.setDoneAction(done);
	
    SpinningWheel.open();
}

function openOneSlot() {
    SpinningWheel.addSlot({
        1: 'Ichi', 
        2: 'Ni', 
        3: 'San', 
        4: 'Shi', 
        5: 'Go'
    });
	
    SpinningWheel.setCancelAction(cancel);
    SpinningWheel.setDoneAction(done);
	
    SpinningWheel.open();
}*/

function done() {
    var results = SpinningWheel.getSelectedValues();
    var word = results.values.join('');
    var r = 'values: ' + results.values.join(' ') + '<br />keys: ' + results.keys.join(', ');   
    $('#result').html(r);

    var matches = getMatches();
    var q = matches.join(", ");
    q = "matches: " + q;
    $('#matches').html(q);    
    
    var yw = '<p id="added">Your word: <input value="'+word+'" /></p>';
    $('#added').html(yw);
}

function cancel() {
    document.getElementById('result').innerHTML = 'cancelled!';
}

window.addEventListener('load', function(){ setTimeout(function(){ window.scrollTo(0,0); }, 100); }, true);

